/**
 * for inquireInfo
 */
$(function(){
	getInquireInfoLists(1);
	// Check all checkboxes when the one in a table head is checked:			
	$('.check-all').click(function(){
		$("#datatable tbody tr").find("input[type='checkbox']").attr('checked', $(this).is(':checked'));   
	});
	
	$(".form-layout .button").click(function(){
		//var dataArray = $("form").serializeArray();		
		getInquireInfoLists(1, null, null, getParamer());
	})

	$(".batchOp").click(function(){
		if($("#batchOp").val() == "Delete"){
			var array = [];
			$("[name='inquireInfoId']:checked").each(function(i){
				array[i] = $(this).val();
			})
			
			if(array.length > 0){
				$.ajax({
					url: ctx+"/inquire/delete.do",
					data : {inquireInfoIds : $.toJSON(array)},
					success: function(){
						getInquireInfoLists(1, null, null, getParamer());
					}
				})
				$(".check-all").attr("checked", false);
			}else{
				showInfo("May selected many.");
			}
		}else{
			showInfo("Please choose a type");
		}
	})
	$(".sort").click(function(){
		var orderBy = $(this).attr("title");
		var orderType = "asc";
		if($(this).hasClass("asc")){
			orderType = "desc";
			$(this).addClass("desc").removeClass("asc");
		}else{
			$(this).addClass("asc").removeClass("desc");
		}
		getInquireInfoLists(1, orderType, orderBy, getParamer());
	})
})
function getParamer(){
	var data = [];
	var consultant = {};
	consultant["property"] = "consultant";
	consultant["value"] = $("#consultant").val();
	consultant["condition"] = "fl";
	if(isNotBlank($("#consultant").val())){
		data[0] = consultant;
	}
	
	var languageId = {};
	languageId["property"] = "l.languageId";
	languageId["value"] = $("#languageId").val();
	languageId["condition"] = "eq";
	if(isNotBlank($("#languageId").val())){
		data[data.length] = languageId;
	}
	
	return $.toJSON(data);
}
function getInquireInfoLists(pageNo, orderType, orderBy, paramdata){	
			if(!pageNo){
				pageNo = 1;
			}
			if(!orderType){
				orderType = "desc";
			}
			if(!orderBy){
				orderBy = "createdOn";
			}
			if(!paramdata){
				paramdata="[]";
			}
			pagination({
				url:ctx+"/inquire/search.do",
				data:{
					pageNo:pageNo, 
					orderType : orderType, 
					orderBy : orderBy, 
					rules : paramdata
				},
				callback: function(data){
					generateInquireInfoLists(data);
					paginationClick();
				}
			})
		}

		function generateInquireInfoLists(data){
			$("#datatable tbody").empty();
			if(data.result){
				$.each(data.result, function(index, rowdata){						
					var view = $('<a href="javascript:void(0)" title="View">查看</a>');
					var del = $('<a href="javascript:void(0)" title="Delete">刪除</a>');
					var tr = $("<tr>")
					.append($("<td>").append($("<input type='checkbox'>").attr("name", "inquireInfoId").val(rowdata.inquireInfoId)).addClass("text-center"))
					.append($("<td>").text(rowdata.consultant))
					.append($("<td>").text(rowdata.consultantCompany))
					.append($("<td>").text(rowdata.consultantTel))
					.append($("<td>").text(rowdata.consultantEmail))
					.append($("<td>").text(rowdata.consultantFax))
					.append($("<td>").text(rowdata.createdOn))
					.append($("<td>").append(view).addClass("text-center op-1"))
					.append($("<td>").append(del).addClass("text-center op-2"));
					$("#datatable tbody").append(tr);
				})
				operation();
			}
		    // Alternating table rows:					
			$('#datatable tbody tr:even').addClass("alt-row"); // Add class "alt-row" to even table rows
		}
        
		function operation(){
			$("#datatable tbody tr").each(function(){
				var inquireInfoId = $(this).find(":checkbox").val();
				$(this).find("a").on("click", function(){
					var title = $(this).attr("title");
					if('View'.equals(title)){
						location.href=ctx+"/inquire/view/"+inquireInfoId+".do";
					}else if("Delete".equals(title)){
						$.ajax({
							url: ctx+"/inquire/delete/"+inquireInfoId+".do",
							success: function(){
								getInquireInfoLists(1);
							}
						})
					}				
				})
			})
		}
       function paginationClick(){
    	   $(".pagination a:not(.disabled):not(.info):not(.current)").on("click",function(){
    		   var pageNo = $(this).attr("data-page");
    		   getInquireInfoLists(pageNo);
    	   })
       }       